package com.wyzz.hr.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.base.UsersAuthority;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IHasAuthBO;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.bo.IWorkFlowBO;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Syslog;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;

public class ReadLog extends BaseAction implements Status {

	public ReadLog() {
		empBo = (IEmployeeBo) getBean("empBo");
		workflowBo = (IWorkFlowBO) getBean("workFlowBO");
		Fathers = new ArrayList();
		empNo = "";
		flowType = "leaverequest";
	}

	public List getLogs(String ID, String tableName, HttpSession session) {
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		List logList = logBO.getLogs(tableName, ID);
		if (logList.size() > 0) {
			Syslog tmpLog = (Syslog) logList.get(0);
			empNo = tmpLog.getSlEmpno();
		}
		if ("recruitplan".equals(tableName)) {
			if (hasAuth(601, new int[0]) || hasAuth(611, new int[] { 3 }))
				return logList;
			if (hasAuth(611, new int[] { 1 })) {
				if (empNo != null && empNo.equals(getCurrentEmpNo(session)))
					return logList;
				if (hasAuth(611, new int[] { 2 }))
					if (checkDeptInCharge(getCurrentEmp(), getDept(empNo)))
						return logList;
					else
						return null;
			}
		}
		if ("tremployeeplan".equals(tableName)) {
			if (hasAuth(301, new int[0]) || hasAuth(311, new int[] { 3 }))
				return logList;
			if (hasAuth(311, new int[] { 1 })) {
				if (empNo.equals(getCurrentEmpNo(session)))
					return logList;
				if (hasAuth(311, new int[] { 2 }))
					if (checkDeptInCharge(getCurrentEmp(), getDept(empNo)))
						return logList;
					else
						return null;
			}
		}
		Employee currentEmp = getCurrentEmp();
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		String fetch[] = { Employee.PROP_EMP_DEPT_NO };
		Employee emp = empBo.loadEmp(empNo, fetch);
		currentEmp = empBo.loadEmp(currentEmp.getId(), fetch);
		if ("leaverequest".equals(tableName)) {
			if (hasAuth(401, new int[0]) || hasAuth(411, new int[] { 3 }))
				return logList;
			if (hasAuth(411, new int[] { 1 })) {
				if (empNo.equals(getCurrentEmpNo(session)))
					return logList;
				if (hasAuth(411, new int[] { 2 }))
					if (empBo.checkEmpInCharge(currentEmp, emp))
						return logList;
					else
						return null;
			}
		}
		if ("overtimerequest".equals(tableName)) {
			if (hasAuth(401, new int[0]) || hasAuth(411, new int[] { 3 }))
				return logList;
			if (hasAuth(411, new int[] { 1 })) {
				if (empNo.equals(getCurrentEmpNo(session)))
					return logList;
				if (hasAuth(411, new int[] { 2 }))
					if (empBo.checkEmpInCharge(currentEmp, emp))
						return logList;
					else
						return null;
			}
		}
		if ("empcompaplan".equals(tableName)) {
			if (hasAuth(201, new int[0]) || hasAuth(211, new int[] { 3 }))
				return logList;
			if (hasAuth(211, new int[0]))
				if (getFather(empNo).contains(getCurrentEmpNo(session)))
					return logList;
				else
					return null;
		}
		return logList;
	}

	public String getCurrentEmpNo(HttpSession session) {
		try {
			return ((Userinfo) session.getAttribute("userinfo")).getId();
		} catch (Exception e) {
			return null;
		}
	}

	public Department getDept(String no) {
		String fetch[] = { "empDeptNo" };
		return empBo.loadEmp(no, fetch).getEmpDeptNo();
	}

	public List getFather(String empNo) {
		if (empBo.loadEmp(empNo, null) == null
				|| empBo.loadEmp(empNo, null).getEmpSupNo() == null
				|| empBo.loadEmp(empNo, null).getEmpSupNo().getId().equals(
						empNo)) {
			return Fathers;
		} else {
			String tempStr = empBo.loadEmp(empNo, null).getEmpSupNo().getId();
			Fathers.add(tempStr);
			empNo = empBo.loadEmp(empNo, null).getEmpSupNo().getId();
			getFather(empNo);
			return Fathers;
		}
	}

	public boolean hasAuth(int moduleId, int condition[]) {
		IHasAuthBO bo = (IHasAuthBO) SpringBeanFactory.getBean("hasService");
		UsersAuthority userAuthority;
		if (condition != null && condition.length > 0)
			userAuthority = new UsersAuthority(String.valueOf(moduleId),
					new String[] { String.valueOf(condition[0]) });
		else
			userAuthority = new UsersAuthority(String.valueOf(moduleId),
					new String[0]);
		boolean has = false;
		try {
			has = bo.hasDWRAuth(userAuthority);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return has;
	}

	IEmployeeBo empBo;
	IWorkFlowBO workflowBo;
	List Fathers;
	String empNo;
	String flowType;
}
